import axios from 'axios'

// 创建axios实例
const service = axios.create({
  baseURL: process.env.API_ROOT, // api 的 base_url
  timeout: 5000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
    /**
     * code为非200是抛错 可结合自己业务进行修改
     */
    const res = response.data

    switch (res.code) {
      case 0:    // 错误
        return response.data
      case 200:
        return response.data
      default:
        return response.data
    }
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
